package com.huadi.ssm.controller;


import com.huadi.ssm.page.Page;
import com.huadi.ssm.po.Achievement;
import com.huadi.ssm.po.Project;
import com.huadi.ssm.po.Researcher;
import com.huadi.ssm.service.AchievementService;
import com.huadi.ssm.service.ProjectService;
import com.huadi.ssm.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 科研成果管理
 *
 */
@RequestMapping("/achievement")
@Controller
public class AchievementController {

    @Autowired
    private AchievementService achievementService;
    @Autowired
    private ProjectService projectService;
    /**
     * 科研成果列表页
     * @param model
     * @return
     */
    @RequestMapping(value="/list",method= RequestMethod.GET)
    public ModelAndView list(ModelAndView model, HttpServletRequest request){
        model.setViewName("achievement/achievement_list");
        Object attribute = request.getSession().getAttribute("userType");
        if("2".equals(attribute.toString())){
            //说明是科研工作人员
            Researcher loginedResearcher = (Researcher)request.getSession().getAttribute("user");
            Long id=loginedResearcher.getId();
            String researchid=id.toString();
            List<Project> projectList = projectService.findByResearchid(researchid);
            model.addObject("projectList",projectList );
            model.addObject("projectListJson", JSONArray.fromObject(projectList));
            return model;
        }
        else{
            List<Project> projectList = projectService.findAll();
            model.addObject("projectList",projectList );
            model.addObject("projectListJson", JSONArray.fromObject(projectList));
            return model;
        }
    }

    /**
     * 获取科研成果列表
     * @param type
     * @param page
     * @return
     */
    @RequestMapping(value="/get_list",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(
            @RequestParam(value="type",required=false,defaultValue="") String type,
            @RequestParam(value="projectid",required=false) Long projectid,
            HttpServletRequest request,
            Page page
    ){
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        Object attribute = request.getSession().getAttribute("userType");
        if("2".equals(attribute.toString())){
            //说明是科研工作人员
            Researcher loginedResearcher = (Researcher)request.getSession().getAttribute("user");
            queryMap.put("researchid", loginedResearcher.getId());
        }
        else{
            queryMap.put("type", "%"+type+"%");
            if(projectid != null){
                queryMap.put("projectid", projectid);
            }
        }
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", achievementService.findList(queryMap));
        ret.put("total", achievementService.getTotal(queryMap));
        return ret;
    }

    /**
     * 编辑科研成果信息
     * @param achievement
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Achievement achievement, HttpServletRequest request){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(achievement.getItem())){
            ret.put("type", "error");
            ret.put("msg", "科研成果名不能为空！");
            return ret;
        }
        if(achievementService.edit(achievement) <= 0){
            ret.put("type", "error");
            ret.put("msg", "科研成果信息修改失败！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "科研成果信息修改成功！");
        return ret;
    }

    /**
     * 添加科研成果信息
     * @param achievement
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Achievement achievement,HttpServletRequest request){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(achievement.getItem())){
            ret.put("type", "error");
            ret.put("msg", "科研成果名不能为空！");
            return ret;
        }
        if(achievementService.add(achievement) <= 0){
            ret.put("type", "error");
            ret.put("msg", "科研成果添加失败！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "科研成果添加成功！");
        return ret;
    }

    /**
     * 删除科研成果信息
     * @param ids
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value="ids[]",required=true) Long[] ids
    ){
        Map<String, String> ret = new HashMap<String, String>();
        if(ids == null || ids.length == 0){
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的数据！");
            return ret;
        }
        try {
            if(achievementService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                ret.put("type", "error");
                ret.put("msg", "删除失败！");
                return ret;
            }
        } catch (Exception e) {
            // TODO: handle exception
            ret.put("type", "error");
            ret.put("msg", "该科研成果下存在其他信息，请勿冲动！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "该科研成果删除成功！");
        return ret;
    }

    @RequestMapping(value="/statslist",method= RequestMethod.GET)
    public ModelAndView statslist(ModelAndView model, HttpServletRequest request){
        model.setViewName("achievement/achievement_statslist");
        Object attribute = request.getSession().getAttribute("userType");
        if("2".equals(attribute.toString())){
            //说明是科研工作人员
            Researcher loginedResearcher = (Researcher)request.getSession().getAttribute("user");
            Long id=loginedResearcher.getId();
            String researchid=id.toString();
            List<Project> projectList = projectService.findByResearchid(researchid);
            model.addObject("projectList",projectList );
            model.addObject("projectListJson", JSONArray.fromObject(projectList));
            return model;
        }
        else{
            List<Project> projectList = projectService.findAll();
            model.addObject("projectList",projectList );
            model.addObject("projectListJson", JSONArray.fromObject(projectList));
            return model;
        }
    }

    @RequestMapping(value="/get_statslist",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getstatsList(
            @RequestParam(value="projectid",required=false) Long projectid,
            HttpServletRequest request
    ){
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("projectid", projectid);
        List<Integer> numberList = new ArrayList<Integer>();
        numberList.add(0);
        numberList.add(0);
        numberList.add(0);
        numberList.add(0);
        List<String> rangeStringList = new ArrayList<String>();
        rangeStringList.add("专利");
        rangeStringList.add("论文");
        rangeStringList.add("软著");
        rangeStringList.add("获奖");
        queryMap.put("type","专利");
        numberList.set(0,achievementService.findForStats(queryMap));
        queryMap.put("type","论文");
        numberList.set(1,achievementService.findForStats(queryMap));
        queryMap.put("type","软著");
        numberList.set(2,achievementService.findForStats(queryMap));
        queryMap.put("type","获奖");
        numberList.set(3,achievementService.findForStats(queryMap));
        Project project=projectService.findByID(projectid);
        ret.put("numberList", numberList);
        ret.put("rangeList", rangeStringList);
        ret.put("projectname",project.getName());
        ret.put("type","suceess");
        return ret;
    }
}
